home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Amiga Amateur Radio User Group / AARUG UK #20 (199x)(Amiga Amateur Radio User Group UK)(PD)[G4DCV].zip / AARUG UK #20 (199x)(Amiga Amateur Radio User Group UK)(PD)[G4DCV].adf / NCOMM / PbConvert.C < prev    next >
C/C++ Source or Header  |  1978-01-09  |  12KB  |  313 lines

  1. /* PbConvert for use with NComm V1.9... */
  2. /* Public Domain by Torkel Lodberg 1990 */
  3.  
  4. #include <stdio.h>
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <proto/intuition.h>
  9. #include <proto/graphics.h>
  10. #include <proto/exec.h>
  11. #include <fcntl.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14.  
  15. #define OLD_FILETYPE_PHONE  5
  16. #define NEW_FILETYPE_PHONE  6
  17.  
  18. static struct pb_Data2 {                 /* V1.8 file format    ***/
  19.     UBYTE        name[41];
  20.     UBYTE        phone[61];
  21.     UBYTE        comment[41];
  22.     UBYTE        pass_word[30];
  23.     UBYTE        script_file[33];
  24.     UBYTE        config_file[33];
  25.     UBYTE        dummy;
  26.     UBYTE       char_set;    /*** 0-11        ***/
  27.     UBYTE        baud;        /*** 0-6 ( 300 - 19200)    ***/
  28.     UBYTE        data_length;    /*** 7 or 8        ***/
  29.     UBYTE        parity;        /*** None, odd, even    ***/
  30.     UBYTE        stop_bits;    /*** 1 - 2        ***/
  31.     UBYTE        duplex;        /*** Full - half    ***/
  32. };
  33.  
  34. static struct pb_Data {                /*** V1.9-> file format  ***/
  35.     UBYTE        name[41];
  36.     UBYTE        phone[61];
  37.     UBYTE        comment[41];
  38.     UBYTE        pass_word[30];
  39.     UBYTE        script_file[33];
  40.     UBYTE        config_file[33];
  41.         UBYTE           macro_file[33];
  42.     UBYTE       char_set;    /*** 0-11        ***/
  43.     UBYTE        baud;        /*** 0-6 ( 300 - 19200)    ***/
  44.     UBYTE        data_length;    /*** 7 or 8        ***/
  45.     UBYTE        parity;        /*** None, odd, even    ***/
  46.     UBYTE        stop_bits;    /*** 1 - 2        ***/
  47.     UBYTE        duplex;        /*** Full - half    ***/
  48.         UBYTE           future[64];
  49. };
  50.  
  51. static int fd = NULL;   /* File handles */
  52. static int fe = NULL;
  53.  
  54. int wb_open;            /* Started from workbench? */
  55.  
  56. void cleanup(text)      /* Exit program cleanly */
  57. char *text;
  58. {
  59.         if (wb_open) printf("\n");
  60.         if (text) printf("%s\n",text);
  61.         if (fd > 0) close(fd);
  62.         if (fe > 0) close(fe);
  63.         if (wb_open) Delay(100);
  64.         exit(0);
  65. }
  66.  
  67. void brk()              /* Called by every CTRL-C / D keypress */
  68. {
  69.         printf("*** BREAK\n");
  70.         cleanup(NULL);
  71. }
  72.  
  73. void main(argc, argv)   /* Main program */
  74. int argc;
  75. char *argv[];
  76. {
  77.     int    status, i;
  78.     char    ch;
  79.         struct  pb_Data data;
  80.         struct  pb_Data2 data2;
  81.  
  82.         char    answer  [256];
  83.         char    infile  [256];
  84.         char    outfile [256];
  85.  
  86.         int     baudchange = NULL;
  87.         int     datachange = NULL;
  88.         int     paritychange = NULL;
  89.         int     stopchange = NULL;
  90.  
  91.         int     oldformat = NULL, removeit = NULL;
  92.         int     baud, data_length, parity, stop_bits;
  93.  
  94.         if (!argc) wb_open = TRUE;
  95.            else wb_open = FALSE;
  96.  
  97.         if (onbreak(&brk)) cleanup(NULL);
  98.         chkabort();
  99.  
  100.         if (argc < 3) {
  101.                 printf("PbConvert for use with NComm V1.9. May remove passwords\n");
  102.                 printf("and filenames from phonebooks, as well as converting the\n");
  103.                 printf("NComm V1.8 file format. Optional global serial changes.\n");
  104.                 printf("Public Domain by Torkel Lodberg 1990\n\n");
  105.  
  106.                 if (argc) {
  107.                    printf("   Usage: PbConvert <infile> <outfile> [-b] [-d] [-p] [-s] [-r] [-e]\n");
  108.                    printf(" Options: [-b]xxxxx where xxxxx is the baudrate (300-19200)\n");
  109.                    printf("          [-d]x where x is the data length (7-8)\n");
  110.                    printf("          [-p]x where x is the parity (n)one, (o)dd, (e)ven\n");
  111.                    printf("          [-s]x where x is the number of stop bits (1-2)\n");
  112.                    printf("          [-r]emove filenames and passwords\n");
  113.                    printf("          [-e]xpect input file to be of old format (v1.8)\n\n");
  114.                    cleanup(NULL);
  115.                 }
  116.         }
  117.  
  118.         if (argc) {
  119.            strcpy(infile, argv[1]);
  120.            strcpy(outfile, argv[2]);
  121.  
  122.            for (i = 3; i < argc; i++) {
  123.                    if (!strncmp(argv[i], "-b", 2)) {
  124.                            baudchange = TRUE;
  125.                            switch (atoi(&argv[i][2])) {
  126.                                    case   300: baud = 0; break;
  127.                                    case   600: baud = 1; break;
  128.                                    case  1200: baud = 2; break;
  129.                                    case  2400: baud = 3; break;
  130.                                    case  4800: baud = 4; break;
  131.                                    case  9600: baud = 5; break;
  132.                                    case 19200: baud = 6; break;
  133.                                       default: cleanup("Invalid baud rate!");
  134.                            }
  135.                    }
  136.                    if (!strncmp(argv[i], "-d", 2)) {
  137.                            datachange = TRUE;
  138.                            switch (atoi(&argv[i][2])) {
  139.                                    case 8: data_length = 0; break;
  140.                                    case 7: data_length = 1; break;
  141.                                   default: cleanup("Invalid data length!");
  142.                            }
  143.                    }
  144.                    if (!strncmp(argv[i], "-p", 2)) {
  145.                            paritychange = TRUE;
  146.                            switch (toupper(argv[i][2])) {
  147.                                    case 'N': parity = 0; break;
  148.                                    case 'O': parity = 1; break;
  149.                                    case 'E': parity = 2; break;
  150.                                     default: cleanup("Invalid parity!");
  151.                            }
  152.                    }
  153.                    if (!strncmp(argv[i], "-s", 2)) {
  154.                            stopchange = TRUE;
  155.                            switch (atoi(&argv[i][2])) {
  156.                                    case 1: stop_bits = 0; break;
  157.                                    case 2: stop_bits = 1; break;
  158.                                   default: cleanup("Invalid number of stop bits!");
  159.                            }
  160.                    }
  161.                    if (!strncmp(argv[i], "-r", 2)) {
  162.                            removeit = TRUE;
  163.                    }
  164.                    if (!strncmp(argv[i], "-e", 2)) {
  165.                            oldformat = TRUE;
  166.                    }
  167.            }
  168.         } else {
  169.            strcpy(infile, "NComm:NComm.phone");
  170.            strcpy(outfile, "RAM:NComm.phone");
  171.  
  172.            printf("Enter name of phonebook input file (Enter=NComm:NComm.phone): ");
  173.            gets(answer);
  174.            if (strlen(answer)) strcpy(infile, answer);
  175.  
  176.            printf("Enter name of phonebook output file (Enter=RAM:NComm.phone): ");
  177.            gets(answer);
  178.            if (strlen(answer)) strcpy(outfile, answer);
  179.  
  180.            oldformat = TRUE;
  181.            printf("\nExpect old file format (Enter=Yes): ");
  182.            gets(answer);
  183.            if (toupper(answer[0]) == 'N') oldformat = FALSE;
  184.  
  185.            printf("\nNew baud rate (Enter=No Change): ");
  186.            gets(answer);
  187.            if (strlen(answer)) {
  188.                 baudchange = TRUE;
  189.                 switch (atoi(answer)) {
  190.                    case   300: baud = 0; break;
  191.                    case   600: baud = 1; break;
  192.                    case  1200: baud = 2; break;
  193.                    case  2400: baud = 3; break;
  194.                    case  4800: baud = 4; break;
  195.                    case  9600: baud = 5; break;
  196.                    case 19200: baud = 6; break;
  197.                    default: cleanup("Invalid baud rate!");
  198.                 }
  199.            }
  200.  
  201.            printf("New data length (Enter=No Change): ");
  202.            gets(answer);
  203.            if (strlen(answer)) {
  204.                 datachange = TRUE;
  205.                 switch (atoi(answer)) {
  206.                    case 8: data_length = 0; break;
  207.                    case 7: data_length = 1; break;
  208.                    default: cleanup("Invalid data length!");
  209.                  }
  210.            }
  211.  
  212.            printf("New parity (Enter=No Change): ");
  213.            gets(answer);
  214.            if (strlen(answer)) {
  215.                 paritychange = TRUE;
  216.                 switch (toupper(answer[0])) {
  217.                    case 'N': parity = 0; break;
  218.                    case 'O': parity = 1; break;
  219.                    case 'E': parity = 2; break;
  220.                    default: cleanup("Invalid parity!");
  221.                 }
  222.            }
  223.  
  224.            printf("New number of stop bits (Enter=No Change): ");
  225.            gets(answer);
  226.            if (strlen(answer)) {
  227.                 stopchange = TRUE;
  228.                 switch (atoi(answer)) {
  229.                    case 1: stop_bits = 0; break;
  230.                    case 2: stop_bits = 1; break;
  231.                    default: cleanup("Invalid number of stop bits!");
  232.                 }
  233.            }
  234.  
  235.            removeit = FALSE;
  236.            printf("\nRemove filenames and passwords (Enter=No): ");
  237.            gets(answer);
  238.            if (toupper(answer[0] == 'Y')) removeit = TRUE;
  239.         }
  240.  
  241.     if((fd = open(infile, O_RDONLY, NULL)) == -1
  242.       ||read (fd, &ch, 1) != 1)
  243.         if (argc) cleanup("Cannot open data-file");
  244.  
  245.         if (oldformat) {
  246.             if (ch != OLD_FILETYPE_PHONE)
  247.                 cleanup("Error reading NComm.phone: Illegal file type");
  248.                 ch = NEW_FILETYPE_PHONE;
  249.         } else {
  250.             if (ch != NEW_FILETYPE_PHONE)
  251.                 cleanup("Error reading NComm.phone: Illegal file type");
  252.         }
  253.  
  254.     if((fe = open(outfile, O_RDWR|O_CREAT|O_TRUNC, NULL)) == -1)
  255.         cleanup("Cannot create file!");
  256.  
  257.     write (fe, &ch, 1);
  258.  
  259.         if (oldformat) {
  260.             while((status = read(fd, (char *) &data2, sizeof(struct pb_Data2))) == sizeof(struct pb_Data2)) {
  261.                         if (removeit) {
  262.                            memset(data2.pass_word, 0, sizeof(data2.pass_word));
  263.                            memset(data2.script_file, 0, sizeof(data2.script_file));
  264.                            memset(data2.config_file, 0, sizeof(data2.config_file));
  265.                         }
  266.  
  267.                         if (baudchange) data2.baud = baud;
  268.                         if (datachange) data2.data_length = data_length;
  269.                         if (paritychange) data2.parity = parity;
  270.                         if (stopchange) data2.stop_bits = stop_bits;
  271.  
  272.                         strcpy(data.name, data2.name);
  273.                         strcpy(data.phone, data2.phone);
  274.                         strcpy(data.comment, data2.comment);
  275.                         strcpy(data.pass_word, data2.pass_word);
  276.                         strcpy(data.script_file, data2.script_file);
  277.                         strcpy(data.config_file, data2.config_file);
  278.                         memset(data.macro_file, 0, sizeof(data.macro_file));
  279.  
  280.                         data.char_set = data2.char_set;
  281.                         data.baud = data2.baud;
  282.                         data.data_length = data2.data_length;
  283.                         data.parity = data2.parity;
  284.                         data.stop_bits = data2.stop_bits;
  285.                         data.duplex = data2.duplex;
  286.                         memset(data.future, 0, sizeof(data.future));
  287.  
  288.                 status = write(fe, (char *) &data, sizeof(struct pb_Data));
  289.                 if(status != sizeof(struct pb_Data))
  290.                                 cleanup("Error while writing!");
  291.                 }
  292.         } else {
  293.             while((status = read(fd, (char *) &data, sizeof(struct pb_Data))) == sizeof(struct pb_Data)) {
  294.                         if (removeit) {
  295.                            memset(data.pass_word, 0, sizeof(data.pass_word));
  296.                            memset(data.script_file, 0, sizeof(data.script_file));
  297.                            memset(data.config_file, 0, sizeof(data.config_file));
  298.                            memset(data.macro_file, 0, sizeof(data.macro_file));
  299.                         }
  300.  
  301.                         if (baudchange) data.baud = baud;
  302.                         if (datachange) data.data_length = data_length;
  303.                         if (paritychange) data.parity = parity;
  304.                         if (stopchange) data.stop_bits = stop_bits;
  305.  
  306.                 status = write(fe, (char *) &data, sizeof(struct pb_Data));
  307.                 if(status != sizeof(struct pb_Data))
  308.                                 cleanup("Error while writing!");
  309.                 }
  310.         }
  311.     cleanup(NULL);
  312. }
  313.